package util;


import org.apache.http.Header;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

public class ClientUtil {

    protected static CloseableHttpClient httpClient = null;
    protected static HttpClientContext httpContext = null;

    static {
        // http与https实现，可在http和https对应操作类中配置代理等
        Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", new MyConnectionSocketFactory())
                .register("https", new MySSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build();

        // http 连接池
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(reg);
        connectionManager.setMaxTotal(1000);
        connectionManager.setDefaultMaxPerRoute(500);
        httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
    }

    public void proxyConfig(){

        httpContext = HttpClientContext.create();

    }

    public CloseableHttpResponse getResp(HttpUriRequest request) {
        try {
            return httpClient.execute(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    // 无body请求
    public HttpUriRequest getHttpUriRequest(String method , String uri , Header[] headers , Map<String, Object> parms) throws UnsupportedEncodingException, URISyntaxException {
        return this.getHttpUriRequest(method, uri, headers, parms,null,null);
    }

    public HttpUriRequest getHttpUriRequest(String method , String uri , Header[] headers , Map<String, Object> parms , String body , String charset) throws  UnsupportedEncodingException, URISyntaxException{
        HttpUriRequest httpUriRequest = null;

        if(charset == null || charset.length() == 0){
            charset = "utf-8";
        }
        if ("get".equalsIgnoreCase(method)) {
            // get请求
            httpUriRequest = get(httpUriRequest,method,uri,headers,parms);
        } else if ("post".equalsIgnoreCase(method)) {
            // post
        }
        return httpUriRequest;
    }

    // get请求
    public static HttpUriRequest get(HttpUriRequest httpUriRequest, String method , String uri , Header[] headers , Map<String, Object> parms){
        URI url = null;
        URIBuilder uriBuilder = null;
        try {
            uriBuilder = new URIBuilder(uri);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        if (null != parms) {
            for (Map.Entry<String, Object> entry : parms.entrySet()) {
                if(	 entry.getValue() instanceof List){
                    List values = (List) entry.getValue();
                    for(Object obj : values ){
                        uriBuilder.addParameter(entry.getKey(), obj==null?"":obj.toString());
                    }

                }else{
                    uriBuilder.addParameter(entry.getKey(), entry.getValue()==null?"":entry.getValue().toString());
                }
            }
        }
        try {
            url = uriBuilder.build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        HttpGet httpGet = new HttpGet(url);
        // 关闭重定向
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(60000)
                .setConnectionRequestTimeout(60000).setSocketTimeout(60000).setRedirectsEnabled(false).build();
        httpGet.setConfig(requestConfig);
        httpUriRequest = httpGet;
        return httpUriRequest;
    }





}
